<?php

namespace app\admin\controller\admin;

use app\admin\controller\Controllers;
use app\admin\model\admin\Role;
use app\exceptions\OrdinaryException;
use app\admin\service\{
    Roles as RoleService,Permission
};

class Roles extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(RoleService $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\Roles.index');
        $config = config('field.roles');
        $list = $service->indexPaginate( $get );
        $this->assign([
            'get' => $get,
            'status' => $config['status'],
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
        ]);
        return $this->fetch('admin/roles/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function create(Permission $permissionService)
    {
        $config = config('field.roles');
        $tree = $permissionService->tree();
        $this->assign([
            'status' => $config['status'],
            'tree' => $tree
        ]);
        return $this->fetch('admin/roles/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function save( RoleService $service )
    {
        $post = $this->request->only(['name','sort_num','description','permission_id','status'], 'post');
        $this->validate($post, 'app\admin\validate\Roles.create');
        $permission_id = $post['permission_id'];
        unset($post['permission_id']);
        $service->create( $post, implode(',', $permission_id) );
        return success();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function edit(Permission $permissionService, RoleService $service, int $id)
    {
        $config = config('field.roles');
        $tree = $permissionService->tree();
        $data = $service->get( $id, true );
        $this->assign([
            'status' => $config['status'],
            'tree' => $tree,
            'data' => $data
        ]);
        return $this->fetch('admin/roles/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function update(RoleService $service, int $id)
    {
        $put = $this->request->only(['name','sort_num','description','permission_id','status'], 'put');
        $this->validate($put, 'app\admin\validate\Roles.update');
        $permission_id = $put['permission_id'];
        unset($put['permission_id']);
        $put['id'] = $id;
        $service->create( $put, implode(',', $permission_id) );
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function delete(RoleService $service, int $id)
    {
        if ( $id <= 0 ) {
            return failed('参数错误,刷新重试!');
        }
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 状态修改
     * @author zx
     * @date 2019-11-14
     * @return \think\Response
     */
    public function status(RoleService $service, Role $model)
    {
        return $service->status($model);
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-11-19
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function destory( RoleService $service )
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\Roles.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }
}
